#coding=utf-8
#!/usr/bin/python

'''
Created on 2012-12-4

@author: Neo.Chen
'''

import socket,select,sys,time
import thread
import os

def loop(cs,addr):
    print '%s %d connected.' % addr
    buffer = cs.recv(22)
    if buffer == '<policy-file-request/>':
    	cs.send('<?xml version="1.0"?>\
<cross-domain-policy>\
<site-control permitted-cross-domain-policies="all"/>\
<allow-access-from domain="*" to-ports="*"/>\
</cross-domain-policy>.\0')
    cs.close()
    sys.exit(0)
             
    
def mainserver(l_port, param):     
    try:    
        ss = socket.socket()
        ss.bind(('0.0.0.0',l_port))
        ss.listen(10)
    except:    
        sys.exit(0)
    
    while True:    
        cs,addr = ss.accept()
        thread.start_new_thread(loop,(cs,addr))
        print "local port: %d" % l_port, "new connect：%s:%d" % addr
    
def main():    
    if True :
        try:
            listen_port = 843
            thread.start_new_thread(mainserver, (listen_port, 0))
            print "proxy starting...listen_port:", listen_port
        except:
            pass
    
    while True:
        time.sleep(1)
        
        
if __name__ == '__main__':
    #PrintLog()
    main()

    
    


























